/*
* Copyright (C) 2004-2010  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

#include "stdafx.h"
#include "FdoWcsSpatialDomain.h"
#include "FdoWcsXmlGlobals.h"
#include "FdoOwsWGS84BoundingBox.h"

FdoWcsSpatialDomain::FdoWcsSpatialDomain(void)
{
    mGridCRS = NULL;
    mBoundingBoxes = FdoOwsBoundingBoxCollection::Create();
}

FdoWcsSpatialDomain::~FdoWcsSpatialDomain(void)
{
}

FdoWcsSpatialDomain* FdoWcsSpatialDomain::Create() 
{
    return new FdoWcsSpatialDomain();
}

void FdoWcsSpatialDomain::Dispose()
{
    delete this;
}

FdoXmlSaxHandler* FdoWcsSpatialDomain::XmlStartElement(FdoXmlSaxContext *context, FdoString *uri, FdoString *name, FdoString *qname, FdoXmlAttributeCollection *atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL)
        {
            if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsDescribeCoverageBBox) == 0)
            {
                FdoOwsBoundingBoxP pBoundingBox = FdoOwsBoundingBox::Create();
				pBoundingBox->InitFromXml(context, atts);

                FdoOwsBoundingBoxCollectionP pBoundingBoxCollection = GetBoundingBoxCollection();
                pBoundingBoxCollection->Add(pBoundingBox);

                pRet = pBoundingBox;
            }
			// The WGS84BoundingBox is a special kind of BoundingBox.
			else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesWGS84BoundingBox) == 0)
			{
				FdoOwsWGS84BoundingBoxP pBoundingBox = FdoOwsWGS84BoundingBox::Create();

                FdoOwsBoundingBoxCollectionP pBoundingBoxCollection = GetBoundingBoxCollection();
                pBoundingBoxCollection->Add(pBoundingBox);

                pRet = pBoundingBox;
			}
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsDescribeCoverageGridCRS) == 0)
            {
                FdoWcsGridCRSP pGridCRS = FdoWcsGridCRS::Create();
                mGridCRS = pGridCRS;

                pRet = mGridCRS;
            }
        }
    }
    catch (FdoException* e)
    {
        context->AddError(e);
        e->Release();
    }

    return pRet;
}

FdoBoolean FdoWcsSpatialDomain::XmlEndElement(FdoXmlSaxContext *context, FdoString *uri, FdoString *name, FdoString *qname)
{
    return BaseType::XmlEndElement(context, uri, name, qname);
}

FdoOwsBoundingBoxCollection* FdoWcsSpatialDomain::GetBoundingBoxCollection() const
{
    return FDO_SAFE_ADDREF(mBoundingBoxes.p);
}

FdoWcsGridCRS* FdoWcsSpatialDomain::GetGridCRS() const
{
    return mGridCRS ? FDO_SAFE_ADDREF(mGridCRS.p) : NULL;
}